package com.india.project.push.otcwms;



import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DealerForHilwmsClientQuarz {
    private static final Log log = LogFactory.getLog("DealerForHilwmsClientQuarz");

    public DealerForHilwmsClientQuarz() {
    }


    // public void dataProcess() {
    //     log.info("******************************........DealerForHilwmsClientQuarz dataProcess() Started To Run ......***********************************");
    //     DBPersistenceManager pm = DBAccessHelper.getPMByName("buwms");
    //     //是否继续执行方法推送
    //     Boolean bo = false;
    //     StringBuffer sqlBuffer = new StringBuffer();
    //     sqlBuffer.append(" SELECT SO_BARCODE_DETAIL.ROW_ID     as ROW_ID ");
    //     sqlBuffer.append("      , SO_BARCODE_DETAIL.ITEMID     as OGPNO ");
    //     sqlBuffer.append("      , SO_BARCODE_DETAIL.SO         as SO");
    //     sqlBuffer.append("      ,SO_BARCODE_DETAIL.SERN       as SERN ");
    //     sqlBuffer.append("      ,SO_BARCODE_DETAIL.MAT_CODE   as MAT_CODE ");
    //     sqlBuffer.append("      , SO_BARCODE_DETAIL.ORDER_TYPE as ORDER_TYPE");
    //     sqlBuffer.append("      , SO_HV_DETAIL.DN_NO    as DN_NO ");
    //     sqlBuffer.append("      ,SO_HV_DETAIL.POSNR           as ITEM_NO ");
    //     sqlBuffer.append(" From  SO_BARCODE_DETAIL, SO_HV_DETAIL ");
    //     sqlBuffer.append(" where (  SO_BARCODE_DETAIL.ITEMID = SO_HV_DETAIL.CNT_BG_CODE(+)    ");
    //     sqlBuffer.append("      AND SO_BARCODE_DETAIL.SO = SO_HV_DETAIL.ORDER_ID(+)  ");
    //     sqlBuffer.append("      AND SO_BARCODE_DETAIL.POSNR = SO_HV_DETAIL.POSNR(+) ");
    //     sqlBuffer.append("      AND  SO_BARCODE_DETAIL.MAT_CODE = SO_HV_DETAIL.MAT_CODE(+)  ");
    //     sqlBuffer.append("      AND  SO_BARCODE_DETAIL.PLANT = SO_HV_DETAIL.PLANT(+))  ");
    //     sqlBuffer.append("      AND  SO_BARCODE_DETAIL.If_Transfer_Otcwms = '0'  ");
    //     sqlBuffer.append("      AND  ROWNUM <= 3000  ");
    //     sqlBuffer.append("   ORDER BY SO_BARCODE_DETAIL.LAST_UPD DESC");
    //     try {
    //         DealerForHilwmsClient client = new DealerForHilwmsClient();
    //         DealerForHilwmsClientPortType port = client.getDealerForHilwmsClientPort();
    //         log.info("read data from DealerForHilwmsClientQuarz.dataProcess() SQL  :" + sqlBuffer.toString());  // 记录sql语句和参数
    //         DataSet ds = (DataSet) pm.executeQuery(sqlBuffer.toString());
    //         ArrayList<StoDtlCheckInfoDTO> list = new ArrayList<StoDtlCheckInfoDTO>();
    //         log.info("查询结束，开始STO推送......");
    //         while (ds.next()) {
    //             StoDtlCheckInfoDTO info = new StoDtlCheckInfoDTO();
    //             info.setOrderNo(ds.getString("SO"));//SO
    //             info.setDn(ds.getString("DN_NO"));
    //             info.setMatCode(ds.getString("MAT_CODE"));
    //             //行项目
    //             info.setItemNo(ds.getString("ITEM_NO"));
    //             info.setBarcode(ds.getString("SERN"));
    //             info.setStatus("0");
    //             list.add(info);
    //         }
    //         if (list.size() > 0) {
    //             //拆分list分为多个小list并方便快速推送
    //             List<List<StoDtlCheckInfoDTO>> lists = this.splitList(list, 500);
    //             log.info("StoDtlCheckInfoDTO{ 本次推送数据：" + list.size() + " 条，共分：" + lists.size() + " 次推送，每次推送 500 条 }");
    //             for (List<StoDtlCheckInfoDTO> stoList : lists) {
    //                 if (stoList.size() > 0) {
    //                     //拼接传送字段
    //                     String barString = this.subBar(stoList);
    //                     if (StringUtils.isNotBlank(barString)) {
    //                         String result = port.saveBarcodesFromSto(stoList);
    //                         if (result.equals("0")) {
    //                             bo = true;
    //                             String updateString = "UPDATE SO_BARCODE_DETAIL T SET T.IF_TRANSFER_OTCWMS = 'Y' WHERE T.IF_TRANSFER_OTCWMS = '0' and T.SERN in (" + barString + ")";
    //                             pm.executeUpdate(updateString);
    //                         } else {
    //                             pm.executeUpdate("UPDATE SO_BARCODE_DETAIL T  SET T.IF_TRANSFER_OTCWMS = 'N'  WHERE T.IF_TRANSFER_OTCWMS = '0' and T.SERN in (" + barString + ")");
    //                             log.info(" 失败条数" + result + " dataProcess end ");
    //                             new Exception("调用otcwms接口失败");
    //                         }
    //                     }
    //                 }
    //             }
    //         } else {
    //             StringBuffer updSqlBuffer0 = new StringBuffer();
    //             updSqlBuffer0.append("  UPDATE SO_BARCODE_DETAIL D  SET D.IF_TRANSFER_OTCWMS = '0'  WHERE D.ORDER_TYPE = 'ZPST' AND D.SAP_FLAG = '1' and D.LAST_UPD >= TO_DATE(TO_CHAR(SYSDATE-10,'YYYY-MM-DD'),'YYYY-MM-DD HH24:MI:SS') AND (D.IF_TRANSFER_OTCWMS IS NULL OR D.IF_TRANSFER_OTCWMS = 'N') ");
    //             updSqlBuffer0.append("   AND EXISTS (SELECT H.*    FROM SO_HV_DETAIL H  WHERE  H.CNT_BG_CODE = D.ITEMID  AND H.POSNR = D.POSNR and H.MAT_CODE = D.MAT_CODE  AND H.PLANT = D.PLANT AND PGI = 'S')");
    //             log.info("read data from DealerForHilwmsClientQuarz.dataProcess() SQL  :" + updSqlBuffer0.toString());  // 记录sql语句和参数
    //             int resultNo = pm.executeUpdate(updSqlBuffer0.toString());
    //             log.info("本次修改数据：" + resultNo + "条");
    //             if (resultNo > 0) {
    //                 bo = true;
    //             }
    //         }
    //         if (bo) {
    //             if (pm != null) {
    //                 pm.close();
    //             }
    //             this.dataProcess();
    //         }
    //     } catch (Exception ex) {
    //         ex.printStackTrace();
    //         // 捕获异常信息
    //         log.info(ex.getMessage());
    //     } finally {
    //         if (pm != null) {
    //             pm.close();
    //         }
    //     }
    //     log.info("******************************........DealerForHilwmsClientQuarz dataProcess() end  ......***********************************");
    // }

    // public void dataProcessByOgpNo(String ogpNo) {
    //     log.info("******************************........DealerForHilwmsClientQuarz dataProcess(String ogpNo) end  ......***********************************");
    //     StringBuffer sqlBuffer = new StringBuffer();
    //     DBPersistenceManager pm = DBAccessHelper.getPMByName("buwms");
    //     sqlBuffer.append(" SELECT SO_BARCODE_DETAIL.ITEMID as ITEMID ");
    //     sqlBuffer.append("      ,SO_BARCODE_DETAIL.SO as SO ");
    //     sqlBuffer.append("      ,SO_BARCODE_DETAIL.SERN as SERN ");
    //     sqlBuffer.append("      ,SO_BARCODE_DETAIL.MAT_CODE as MAT_CODE ");
    //     sqlBuffer.append("      ,SO_BARCODE_DETAIL.ORDER_TYPE as ORDER_TYPE ");
    //     sqlBuffer.append("      ,SO_BARCODE_DETAIL.PLANT as PLANT ");
    //     sqlBuffer.append("      ,SO_BARCODE_DETAIL.SAP_FLAG as SAP_FLAG ");
    //     sqlBuffer.append("      ,SO_BARCODE_DETAIL.CAR_NO as CAR_NO ");
    //     sqlBuffer.append("      ,SO_BARCODE_DETAIL.UNIT as UNIT ");
    //     sqlBuffer.append("      ,SO_BARCODE_DETAIL.INVOICE_NO as INVOICE_NO ");
    //     sqlBuffer.append("      ,replace(SO_DETAIL.MAT_DESC, chr(34), '') as MAT_DESC ");
    //     sqlBuffer.append("      ,SO_BARCODE_DETAIL.CON_NO as CON_NO ");
    //     sqlBuffer.append("      ,SO_HV_DETAIL.DN_NO ");
    //     sqlBuffer.append("      ,SO_HV_DETAIL.POSNR as ITEM_NO ");
    //     sqlBuffer.append(" From  SO_BARCODE_DETAIL ");
    //     sqlBuffer.append("       ,SO_DETAIL ");
    //     sqlBuffer.append("       ,CD_MAT_INFO ");
    //     sqlBuffer.append("       ,SO_HV_DETAIL ");
    //     sqlBuffer.append(" where (SO_BARCODE_DETAIL.SO = SO_DETAIL.SO(+) ");
    //     sqlBuffer.append("      AND SO_BARCODE_DETAIL.POSNR = SO_DETAIL.POSNR(+)  ");
    //     sqlBuffer.append("      AND SO_BARCODE_DETAIL.PLANT = CD_MAT_INFO.PLANT(+)  ");
    //     sqlBuffer.append("      AND SO_BARCODE_DETAIL.MAT_CODE = CD_MAT_INFO.MAT_CODE(+)  ");
    //     sqlBuffer.append("      AND SO_BARCODE_DETAIL.ITEMID = SO_HV_DETAIL.CNT_BG_CODE(+)  ");
    //     sqlBuffer.append("      AND SO_BARCODE_DETAIL.SO = SO_HV_DETAIL.ORDER_ID(+)  ");
    //     sqlBuffer.append("      AND SO_BARCODE_DETAIL.POSNR = SO_HV_DETAIL.POSNR(+)  ");
    //     sqlBuffer.append("      AND SO_BARCODE_DETAIL.MAT_CODE = SO_HV_DETAIL.MAT_CODE(+)  ");
    //     sqlBuffer.append("      AND SO_BARCODE_DETAIL.PLANT = SO_HV_DETAIL.PLANT(+)  )");
    //     sqlBuffer.append("      AND ( SO_BARCODE_DETAIL.ORDER_TYPE = 'ZPST' ");
    //     sqlBuffer.append("                     AND SO_HV_DETAIL.PGI = 'S' ");
    //     sqlBuffer.append("                     AND SO_BARCODE_DETAIL.SAP_FLAG = '1' ");
    //     sqlBuffer.append("                     and (SO_BARCODE_DETAIL.If_Transfer_Otcwms is null or SO_BARCODE_DETAIL.If_Transfer_Otcwms = 'N')");
    //     sqlBuffer.append("                  )");
    //     sqlBuffer.append("   AND  SO_BARCODE_DETAIL.ITEMID = '" + ogpNo + "'  ");
    //     sqlBuffer.append("   ORDER BY SO_BARCODE_DETAIL.LAST_UPD ASC");
    //     // 记录sql语句和参数
    //     log.info("read data from DealerForHilwmsClientQuarz.dataProcess()  SQL :" + sqlBuffer.toString());
    //     String successNum = "0";
    //     String failNum = "0";
    //     try {
    //         DealerForHilwmsClient client = new DealerForHilwmsClient();
    //         DealerForHilwmsClientPortType port = client.getDealerForHilwmsClientPort();
    //         DataSet ds = (DataSet) pm.executeQuery(sqlBuffer.toString());
    //         ArrayList<StoDtlCheckInfoDTO> list = new ArrayList<StoDtlCheckInfoDTO>();
    //         while (ds.next()) {
    //             StoDtlCheckInfoDTO info = new StoDtlCheckInfoDTO();
    //             info.setOrderNo(ds.getString("SO"));//SO
    //             info.setDn(ds.getString("DN_NO"));
    //             info.setMatCode(ds.getString("MAT_CODE"));
    //             info.setItemNo(ds.getString("ITEM_NO"));
    //             info.setBarcode(ds.getString("SERN"));
    //             info.setStatus("0");
    //             list.add(info);
    //         }
    //         String result = port.saveBarcodesFromSto(list);
    //         failNum = result;
    //         if (result.equals("0")) {
    //             String updateString = "UPDATE SO_BARCODE_DETAIL T SET T.IF_TRANSFER_OTCWMS = 'Y' WHERE T.ITEMID='" + ogpNo + "'";
    //             pm.executeUpdate(updateString);
    //             successNum = list.size() + "";
    //         } else {
    //             failNum = result;
    //             new Exception("调用otcwms接口失败");
    //         }
    //     } catch (Exception ex) {
    //         ex.printStackTrace();
    //         log.info(ex.getMessage());
    //     }
    //     log.info(" 成功保存条数 " + successNum + ",失败条数" + failNum + " dataProcess end ");
    //     log.info("******************************........DealerForHilwmsClientQuarz dataProcess(String ogpNo) end  ......***********************************");
    // }

    /**
     * 按指定大小，分隔集合，将集合按规定个数分为n个部分
     *
     * @param list
     * @param len  指定拆分后小list大小
     * @return
     */
    public <T> List<List<T>> splitList(List<T> list, int len) {
        //判断非空
        if (list == null || list.isEmpty() || len < 1) {
            return Collections.emptyList();
        }
        //声明返回对象存值
        List<List<T>> result = new ArrayList<List<T>>();
        int size = list.size();
        //计算循环次数
        int count = (size + len - 1) / len;
        for (int i = 0; i < count; i++) {
            List<T> subList = list.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
            result.add(subList);
        }
        return result;
    }

    // public String subBar(List<StoDtlCheckInfoDTO> stoList) {
    //     StringBuffer str = new StringBuffer();
    //     String result = null;
    //     if (stoList.size() > 0) {
    //         for (StoDtlCheckInfoDTO stoDtlCheckInfoDTO : stoList) {
    //             str.append("'").append(stoDtlCheckInfoDTO.getBarcode()).append("',");
    //         }
    //         result = str.substring(0, str.length() - 1);
    //     }
    //     return result;
    // }
}
